package de.au.fi.imp.reader;

import org.openqa.selenium.By;

public class OnVistaReader extends WebReader {

	String onVistaId;

	public OnVistaReader() {
		getFirefoxDriver();
		driver.get("http://www.onvista.de");
		wait(1);
	}

	public void searchForWkn(String wkn) {

		setElementTo(By.name("SEARCH_VALUE"));
		click();
		element.sendKeys(wkn);
		setElementTo(By.name("doSubmit"));
		click();
		setElementTo(By.xpath("/html/body/div/div/div/div/h2"));
		if(element !=null && "Suchergebnis".contains(getText().trim()))
		{
			setElementTo(By.xpath("/html/body/div/div/div/div/div[3]/table/tbody/tr[2]/td[2]/a"));
			click();
		}
		getOnVistaIdFromUrl(driver.getCurrentUrl());
	}

	private void getOnVistaIdFromUrl(String currentUrl) {
		if(currentUrl==null)return;
		onVistaId = currentUrl.substring(currentUrl.indexOf("=")+1);
	}
	
	public String getOnVistaId()
	{
		return onVistaId;
	}

	public void openCharts() {
		driver.get("http://www.onvista.de/aktien/charts.html?ID_OSI="+onVistaId);
	}

	public void openKennzahlen() {
		driver.get("http://www.onvista.de/aktien/kennzahlen/fundamental.html?ID_OSI="+onVistaId);
	}

	public String getChartsKursForDateString(String dateString) {
		setElementTo(By
				.xpath("/html/body/div/div/div[2]/form[3]/div/div/div/input"));
		element.sendKeys(dateString);
		setElementTo(By
				.xpath("/html/body/div/div/div[2]/form[3]/div/div/div/input[2]"));
		click();
		setElementTo(By
				.xpath("/html/body/div/div/div[2]/form[3]/div/div/table/tbody/tr[2]/td[5]"));
		return logElementText("Kurs " + dateString);
	}

	@Deprecated
	String getBasicKursNow() {
		setElementTo(By
				.xpath("/html/body/div/div/div[3]/div[8]/table/tbody/tr[2]/td/span"));
		return logElementText("Kurs");
	}

	public String getBasicBranche() {
		setElementTo(By
				.xpath("/html/body/div/div/div[3]/div/table/tbody/tr/td[6]/a"));
		return logElementText("branche");
	}

	public String getBasicIsin() {
		setElementTo(By
				.xpath("/html/body/div/div/div[3]/div/table/tbody/tr/td[4]"));
		return logElementText("ISIN");
	}
	
	public String getBasicName()
	{	
		setElementTo(By.xpath("/html/body/div/div/div[3]/h1"));
		return logElementText("Name");
	}
	
	public String getKennzahlenMarktKap() {
		setElementTo(By
				.xpath("/html/body/div/div/div/div[2]/table/tbody/tr/td[2]"));
		return logElementText("marktkap.");
	}
	
	public String getKennzahlenEpsAj()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[2]/td[3]"));
		return logElementText("EpsAj");
		
	}
	
	public String getKennzahlenEpsNj()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[2]/td[2]"));
		return logElementText("EpsNj");
		
	}
	
	public String getKennzahlenEbit()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[14]/table/tbody/tr[3]/td[2]"));
		return logElementText("ebit");
	}
	
	public String getKennzahlenRoe()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[14]/table/tbody/tr[5]/td[2]"));
		return logElementText("ROE");
	}
	
	public String getKennzahlenEigenKapQuote()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[12]/table/tbody/tr[3]/td[2]"));
		return logElementText("EigenKapQuote");
	}
	
	public String getKennzahlenDividende()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[8]/table/tbody/tr[2]/td[3]"));
		return logElementText("Dividende");
	}
	
	public String getKennzahlenKgvNj()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[3]/td[2]"));
		return logElementText("KGV NJ");
	}
	
	public String getKennzahlenKgvAj()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[3]/td[3]"));
		return logElementText("Kgv Aj");
	}
	
	public String getKennzahlenKgv1j()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[3]/td[4]"));
		return logElementText("Kgv 1j");
		
	}
	
	public String getKennzahlenKgv2j()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[3]/td[5]"));
		return logElementText("Kgv 2j");
	}
	
	public String getKennzahlenKgv3j()
	{
		setElementTo(By.xpath("//div[@id='ONVISTA']/div[1]/div[1]/div[7]/table/tbody/tr[3]/td[6]"));
		return logElementText("Kgv 3j");
	}
	
}